package edu.pku.sei.transformation.semantics.match;

import choco.kernel.model.variables.VariableType;
import choco.kernel.model.variables.integer.IntegerVariable;
import edu.pku.sei.mdabase.core.ICSPModelManager;
import edu.pku.sei.mdabase.infrastructure.model.ModelManager;


public class ModelVariable extends IntegerVariable {
	private int[] CONSTANT_VALUE = new int[]{0};
	
	
	public ModelVariable(String name,Object modelType,VariableType type,ICSPModelManager modelManager){
		super(name,type,ModelManager.EMPTY_VALUE);
		this.addOption("cp:enum");
		this.modelManager = modelManager;
		this.modelType = modelType;
	}
	
	public ModelVariable(String name, Object modelType,
			ICSPModelManager manager) {
		// TODO Auto-generated constructor stub
		this(name,modelType,VariableType.INTEGER,manager);
	}

	public void updateValues(Object ... param){
		
		this.values = modelManager.getValuesOf(modelType,param);
		
		int min=-1,max=-1;
		
		for(int i : values){
			if(min==-1)
				min = i;
			if(max==-1)
				max = i;
			if(min>i) min = i;
			if(max<i) max = i;
		}
		
		this.setLowB(min);
		this.setUppB(max);
	}
	
	public boolean isValid(){
		if(this.values.length==1&&this.values[0]==-1)
			return false;
		
		return true;
	}
	
	public void updateValue(int value){
		
		CONSTANT_VALUE[0] = value;
		this.values = CONSTANT_VALUE;
		this.setLowB(value);
		this.setUppB(value);
	}
	
	public int newInstance(){
		return modelManager.createElement(modelType);
	}

	public void deleteInstance(int id){
		modelManager.deleteElement(id);
	}
	protected ICSPModelManager modelManager;
	protected Object modelType;


	private boolean fixed = false;
	public boolean isFixed() {
		return fixed;
	}

	public void setFixed(boolean f) {
		this.fixed = f;
	}
}
